package com.timo.lottery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: Timo
 * @Date: 2019/5/31 09:25
 * @Description: 权重抽奖
 *
 */
public class WeightsLottery {
    /**
     * 奖品
     */
    private Map<Integer, Prize> prizeMap;



    public WeightsLottery(List<Prize> prizeList) {
        prizeMap = new HashMap<>();
        prizeList.forEach(p->{
            prizeMap.put(p.getId(), p);
        });
    }

    /**
     * 抽奖
     * @param userName
     * @return
     */
    public String lottery(String userName) {
        return "";
    }

    /**
     * 获取总权重，根据每个奖品的权重相加起来
     * @return
     */
    private int weightTotal() {
        AtomicInteger total = new AtomicInteger();
        prizeMap.forEach((k,v) ->{
            total.addAndGet(v.getPrizeWeight());
        });
        return total.get();
    }

    private synchronized void lessPrizel(Integer id) {
        Prize prize = prizeMap.get(id);
        if (prize.getPrizeAmount() > 0) {
            prize.setPrizeAmount(prize.getPrizeAmount()-1);
        }
        prizeMap.put(id, prize);
    }




}
